<?php

	include("common.php");
	
	if(!empty($_POST)){
		mysql_query("INSERT INTO `197_guestbook` ( `title`, `content`, `ip`, `name`, `post_date` ) VALUES ( '" . $_POST['title'] . "', '" . $_POST['content'] . "', '" . $_SERVER['REMOTE_ADDR'] . "', '" . $_POST['name'] . "', '" . date("Y-m-d") . "' )");
	
		include("header.php");
?>
<?php if(!AJAX){ ?><h1>張貼留言</h1><?php } ?>
<p>張貼成功，請<a href="guestbook.php">按此</a>返回。</p>
<?php

		include("sidebar.php");
		include("footer.php");
		
		exit;

	}
	
	$quote = false;
	$quote_content = array();
	if(isset($_GET['guestbook_id'])){
		$result = mysql_query("SELECT * FROM `197_guestbook` WHERE `guestbook_id` = " . $_GET['guestbook_id']);
		if(mysql_num_rows($result)){
			$quote = true;
			$quote_content = mysql_fetch_assoc($result);
		}
	}
	
?>
<?php include("header.php"); ?>
<script type="text/javascript">
	$(function(){
		$('textarea.tinymce').tinymce({
			// Location of TinyMCE script
			script_url : './jquery/tiny_mce/tiny_mce.js',

			// General options
			theme : "advanced",
			skin : "o2k7",
			plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",

			// Theme options
			theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,fontselect,fontsizeselect",
			theme_advanced_buttons2 : "bullist,numlist,|,blockquote,|,undo,redo,|,link,unlink,image,code,styleprops,forecolor,backcolor",
			theme_advanced_buttons3 : "hr,removeformat,visualaid,|,charmap,emotions,iespell,media,|,print,|,fullscreen",
			theme_advanced_buttons4 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,

			// Drop lists for link/image/media/template dialogs
			template_external_list_url : "lists/template_list.js",
			external_link_list_url : "lists/link_list.js",
			external_image_list_url : "lists/image_list.js",
			media_external_list_url : "lists/media_list.js"
		});
	});
</script>
<script type="text/javascript" defer="defer">
	function addGuestbook(){
		$("#add-result").dialog("option", "buttons", { "關閉": function(){ location.replace("guestbook.php"); } });
		$("#add-result").html("<img src='../images/ajax-loader.gif' />");
		$("#add-result").dialog("open");
		$.post("guestbookAdd.php?ajax=true", { title: $("#title").val(), content: $("#content").val(), name: $("#name").val() }, function(r){ $("#add-result").html(r) });
	}
	function exposeGuestbookForm(){
		$("#guestbook-form").expose({
			// when exposing is done, change form's background color
			onLoad: function() {
				$("#guestbook-form").animate({backgroundColor: '#c7f8ff'});
			},

			// when "unexposed", return to original background color
			onClose: function() {
				$("#guestbook-form").animate({backgroundColor: '#FFFFFF'});
			}
		});
	}
</script>
<form id="guestbook-form" onsubmit="addGuestbook(); return false;" onclick="exposeGuestbookForm();" action="post.php" method="POST" style="padding: 10px;">
	<h1>張貼留言</h1>
	<p><label>標題：<br /><?php if(!$quote){ ?><input type="text" name="title" id="title" /><?php } else { ?><input type="hidden" value="Re: <?php echo $quote_content['title']; ?>" name="title" id="title" />Reply: <?php echo $quote_content['title']; } ?></label></p>
	<p><label>內容：<br /><textarea name="content" onclick="exposeGuestbookForm();" id="content" style="width: 450px;" class="tinymce"><?php if($quote){ ?><div style="color: #AAA; font-size: .95em; padding-left: 10px;"><p><strong>※引用 <?php echo $quote_content['name']; ?> 之銘言※</strong></p><?php echo $quote_content['content']; ?></div><p></p><?php } ?></textarea></label></p>
	<p><label>大名：<br /><input type="text" name="name" id="name" /></label></p>
	<p><input type="submit" value="送出" /></p>
	<div id="add-result"></div>
</form>
<?php include("sidebar.php"); ?>
<?php include("footer.php"); ?>